package per.zqx.routing;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DeliverCallback;
import per.zqx.utils.RabbitUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Customer02
 *
 * @author Sa  2021-06-02 18:21
 */
public class Customer02 {
    // 消费者绑定的队列
    private static final String QUEUE_NAME_2 = "queue_name_2";

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = RabbitUtils.getConnection();
        Channel channel = connection.createChannel();
        // 声明队列
        channel.queueDeclare(QUEUE_NAME_2, true, false, false, null);
        // 队列绑定交换机的多个路由键
        channel.queueBind(QUEUE_NAME_2, Producer.EXCHANGE_NAME, Producer.ROUTER_KEY_2);
        channel.queueBind(QUEUE_NAME_2, Producer.EXCHANGE_NAME, Producer.ROUTER_KEY_3);
        // 同时只能消费一个消息
        channel.basicQos(1);
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            byte[] body = message.getBody();
            String msg = new String(body, "utf-8");
            System.out.println("Customer2 received a message -> " + msg);
        };
        boolean autoAck = true;
        channel.basicConsume(QUEUE_NAME_2, autoAck, deliverCallback, consumerTag -> {
        });
    }
}
